import itertools
import logging
import sys
import zmq

logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)

REQUEST_TIMEOUT = 2500
REQUEST_RETRIES = 3
SERVER_ENTPONIT = "tcp://localhost:5555"

context = zmq.Context()

logging.info("Connecting to server...")
client = context.socket(zmq.REQ)
client.connect(SERVER_ENTPONIT)

for sequence in itertools.count():
    request = str(sequence).encode()
    logging.info("Sending (%s)" % request)
    client.send(request)

    retries_left = REQUEST_RETRIES
    while True:
        if (client.poll(REQUEST_TIMEOUT) & zmq.POLLIN) != 0:
            reply = client.recv()
            if int(reply) == sequence:
                logging.info("Server replied OK (%s)" % reply)
                break
            else:
                logging.error("Malformed reply from server: %s" % reply)
                continue
        retries_left -= 1
        logging.warning("No response from server")
        client.setsockopt(zmq.LINGER, 0)
        client.close()
        if retries_left == 0:
            logging.error("Server seems to be offline, abandoning")
            sys.exit()
        logging.info("Reconnecting to server...")
        client = context.socket(zmq.REQ)
        client.connect(SERVER_ENTPONIT)
        logging.info("Resending (%s)", request)
        client.send(request)
